package com.softserve.academy.food.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

import com.softserve.academy.food.model.mHistoryOrders;
import com.softserve.academy.food.model.mOrder;


@Entity
@Table( name = "HISTORY_ORDERS" )
public class eHistoryOrders 
{
	@Id
	@GeneratedValue(generator="increment")
	@GenericGenerator(name="increment", strategy = "increment")
	private int id;
	
	@ManyToOne(fetch = FetchType.LAZY)
	private eUser user;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "history" )
	private List<eOrder> orders = new ArrayList<eOrder>();
	
	@Temporal(TemporalType.DATE)
	@Column(name = "date")
	private Date data;
	
	private String description;
	
	
	public eHistoryOrders()
	{
	}
	 
	public eHistoryOrders( mHistoryOrders history )
	{
		List<eOrder> orders = new ArrayList<eOrder>();
		id = history.getId();
		
		for (mOrder order : history.getOrders())
		{
			orders.add( new eOrder(order) );
		}
		this.orders = orders;

		user = new eUser( history.getUser() );
		description = history.getDescription();
		data = history.getData();
	}

	public int getId() 
	{
		return id;
	}

	public void setId(int id) 
	{
		this.id = id;
	}

	public eUser getUser() 
	{
		return user;
	}

	public void setUser(eUser user) 
	{
		this.user = user;
	}

	public List<eOrder> getOrders() 
	{
		return orders;
	}

	public void setOrders(List<eOrder> orders) 
	{
		this.orders = orders;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) 
	{
		this.description = description;
	}

	public Date getData() 
	{
		return data;
	}

	public void setData(Date data) 
	{
		this.data = data;
	}

	@Override
	public int hashCode() 
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((data == null) ? 0 : data.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + id;
		result = prime * result + ((orders == null) ? 0 : orders.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) 
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof eHistoryOrders))
			return false;
		eHistoryOrders other = (eHistoryOrders) obj;
		if (data == null) 
		{
			if (other.data != null)
				return false;
		} 
		else if (!data.equals(other.data))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} 
		else if (!description.equals(other.description))
			return false;
		if (id != other.id)
			return false;
		if (orders == null) 
		{
			if (other.orders != null)
				return false;
		} 
		else if (!orders.equals(other.orders))
			return false;
		if (user == null) 
		{
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

	@Override
	public String toString() 
	{
		return "eHistoryOrders [id=" + id + ", user=" + user + ", orders="
				+ orders + ", description=" + description + ", data=" + data
				+ "]";
	}



}
